const clientService = require("../services/client-service.js");
class ClientController {
  // -----логин----------------
  async login(req, res, next) {
    try {
      const { email, password } = req.body;
      const clientData = await clientService.login(email, password);
      res.cookie("refreshToken", clientData.refreshToken, {
        maxAge: 30 * 24 * 60 * 60 * 1000,
        httpOnly: true,
      });
      return res.json({ clientData, message: "вы вошли" });
    } catch (e) {
      next(e);
    }
  }
  // -----выход----------------
  async logout(req, res, next) {
    try {
      const { refreshToken } = req.cookies;
      const token = await clientService.logout(refreshToken);
      res.clearCookie("refreshToken");
      return res.json({ token, message: "вы вышли" });
    } catch (e) {
      next(e);
    }
  }
  // ----- добавление информации о клиенте----------------
  async addInfo(req, res, next) {
    const { email, fullName } = req.body;
    const client = await clientService.addInfo(email, fullName);

    return res.json({ message: "Информация добавлена", client });
  }
  // -------сброс пароля----------------
  async resetPassword(req, res, next) {
    try {
      const { email } = req.body;
      const clientData = await clientService.resetPassword(email);
      return res.json({
        clientData,
        message: "Ссылка для сброса пароля отправлена на почту",
      });
    } catch (e) {
      next(e);
    }
  }
  // -----сброс пароя--------------
  async changePassword(req, res, next) {
    try {
      const { oldpassword, newpassword } = req.body;
      const { token } = req.params;
      const clientData = await clientService.changePassword(
        token,
        oldpassword,
        newpassword
      );
      res.json({ clientData, message: "Пароль изменен" });
    } catch (e) {
      next(e);
    }
  }
  // --------изменение пароля-----------------
  async changePass(req, res, next) {
    try {
      const { oldpassword, newpassword, accessToken } = req.body;
      const clientData = await clientService.changePass(
        accessToken,
        oldpassword,
        newpassword
      );
      res.json({ clientData, message: "Пароль изменен" });
    } catch (e) {
      next(e);
    }
  }
  // ------- активация аккаунта---------------
  async activate(req, res, next) {
    try {
      const activationLink = req.params.link;
      await clientService.activate(activationLink);
      return res.redirect(process.env.CLIENT_URL);
    } catch (e) {
      next(e);
    }
  }
  // -------рефреш и аксес токен обновление
  async refresh(req, res, next) {
    try {
      const { refreshToken } = req.cookies;
      const clientData = await clientService.refresh(refreshToken);
      // await clientService.refresh(refreshToken);
      res.cookie("refreshToken", clientData.refreshToken, {
        maxAge: 30 * 24 * 60 * 60 * 1000,
        httpOnly: true,
      });
      return res.json(clientData);
    } catch (e) {
      next(e);
    }
  }
}

module.exports = new ClientController();
